package spittr.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter
{
    @Autowired
    ApplicationContext applicationContext;

    @Bean
    // 视图解析器
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine)
    {
        // InternalResourceViewResolver resolver = new
        // InternalResourceViewResolver("/WEB-INF/views/", ".jsp");
        // resolver.setViewClass(JstlView.class);
        TilesViewResolver resolver = new TilesViewResolver();
        // ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        // resolver.setTemplateEngine(templateEngine);
        return resolver;
    }

    @Bean
    // 模板引擎
    public TemplateEngine templateEngine(ITemplateResolver templateResolver)
    {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;
    }

    @Bean
    // 模板解析器
    public ITemplateResolver templateResolver()
    {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setApplicationContext(applicationContext);
        return templateResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    {
        configurer.enable();
    }

    @Bean
    public MessageSource messageSource()
    {
        /**
         * ResourceBundleMessageSource 只能在类的根路径查找
         */
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("messages");
        messageSource.setCacheSeconds(10);

        /**
         * ReloadableResourceBundleMessageSource 可以在外部路径查找<br>
         * 1.classpath:<br>
         * 2.file:<br>
         * 3.Web应用的根路径
         */
        ReloadableResourceBundleMessageSource messageSource2 = new ReloadableResourceBundleMessageSource();
        messageSource2.setBasename("classpath:messages");
        messageSource2.setCacheSeconds(10);
        return messageSource;
    }

    @Bean
    public TilesConfigurer tilesConfigurer()
    {
        TilesConfigurer configurer = new TilesConfigurer();
        configurer.setDefinitions("/WEB-INF/layout/tiles.xml",
                // 可以使用 ant风格 通配符
                "/WEB-INF/**/tiles.xml");
        // 启动刷新功能
        configurer.setCheckRefresh(true);
        return configurer;
    }
}
